let vm = new Vue({
    el: '.main-content',
    data: function () {
        return {
            name: "全部",
            condition: {
                remarks: ''
            },
            setting: {
                data: {
                    simpleData: {
                        enable: true,
                        pIdKey: "parentId"
                    },

                },
                check: {
                    enable: true,
                    chkboxType: {"Y": "ps", "N": "ps"}
                },
                callback: {
                    onClick: this.onClick
                },
                view: {
                    fontCss: this.fontCss
                }
            },
            nodes: [],
            treeObj: {},
            role: {},
            officeTreeObj: '',
            officeNodes: [],
            flag: false,
            params: {
                role: '',
                _resources: [],
                resources: [],
                _offices: [],
                offices: []
            }
        }
    },
    methods: {

        update: function () {
            let resources = this.treeObj.getCheckedNodes(true);
            if (resources.length > 0 && resources[0].id == 0) {
                resources.splice(0, 1);
            }
            for (let i = 0; i < resources.length; i++) {
                this.params.resources.push(resources[i].id);
            }


            if (this.officeTreeObj != '') {
                let offices = this.officeTreeObj.getCheckedNodes(true);
                if (offices.length > 0 && offices[0].id == 0) {
                    offices.splice(0, 1);
                }
                for (let i = 0; i < offices.length; i++) {
                    this.params.offices.push(offices[i].id);
                }
            } else {
                if (this.flag) {
                    //  如果删除标记为true  表示原来有值 现在要删除  office授权
                    this.params.offices = [];
                } else {
                    //新旧一致
                    this.params.offices = this.params._offices;
                }
            }

            this.params.role = this.role

            axios({
                url: 'mapper/role/update',
                method: 'post',
                data: this.params
            }).then(response => {
                if (response.data.success) {
                    //根据当前窗体名字获取其索引值
                    let index = parent.layer.getFrameIndex(window.name);
                    console.log(index);
                    //根据索引值关闭子窗口
                    parent.layer.close(index);
                } else {
                    layer.msg(response.data.msg);
                }
            }).catch(function (error) {
                layer.msg(error.message);
            })


        },
        changeDataScope: function () {
        },

        initTree: function () {
            //初始化ztree
            //获取nodes
            axios({
                url: 'mapper/resource/select'
            }).then(response => {
                this.nodes = response.data.obj;
                this.nodes[this.nodes.length] = {"id": 0, "name": "所有权限",}

                this.treeObj = $.fn.zTree.init($("#select-treetreeSelectResEdit"), this.setting, this.nodes);

                this.selectById();
            }).catch(function (error) {
                layer.msg(error.message);
            });

        },

        initOfficeTree: function () {
            //初始化nodes数据
            axios({
                url: "mapper/office/selectAll"
            }).then(response => {
                this.nodes = response.data.obj;
                this.nodes[this.nodes.length] = {"id": 0, "name": "所有机构"};
                this.officeTreeObj = $.fn.zTree.init($("#select-treetreeSelectOfficeEdit"), this.setting, this.nodes);
                $("#treeSelectOfficeEdit").css("display", "inline-block");//设置显示
                this.selectOfficesById();
            }).catch(error => {
                layer.msg(error.message)
            })
        },
        selectById: function () {
            axios({
                url: 'mapper/resource/selectById',
                params: {rid: this.role.id}
            }).then(response => {

                let _nodes = response.data.obj;

                for (let i = 0; i < _nodes.length; i++) {
                    this.params._resources.push(_nodes[i].id);
                }

                let transformNodes = this.treeObj.transformToArray(this.treeObj.getNodes());
                for (let i in _nodes) {
                    for (let j in transformNodes) {
                        if (_nodes[i].id == transformNodes[j].id) {
                            transformNodes[j].checked = true;
                            this.treeObj.updateNode(transformNodes[j]);
                            break;
                        }
                    }
                }
            }).catch(function (error) {
                layer.msg(error.message);
            });
        },
        selectOfficesById: function () {
            axios({
                url: 'mapper/office/selectById',
                params: {rid: this.role.id}
            }).then(response => {

                let _nodes = response.data.obj;

                for (let i = 0; i < _nodes.length; i++) {
                    this.params._offices.push(_nodes[i].id);
                }

                let transformNodes = this.officeTreeObj.transformToArray(this.officeTreeObj.getNodes());
                for (let i in _nodes) {
                    for (let j in transformNodes) {
                        if (_nodes[i].id == transformNodes[j].id) {
                            transformNodes[j].checked = true;
                            this.officeTreeObj.updateNode(transformNodes[j]);
                            break;
                        }
                    }
                }
            }).catch(function (error) {
                layer.msg(error.message);
            });
        },


        onClick: function (event, treeId, treeNode) {
            let zTreeObj = $.fn.zTree.getZTreeObj("select-treetreeSelectResEdit");  //根据树id获取树对象
            //获取所有树节点，多维结构
            let nodes = zTreeObj.getNodes();
            //转换成一维结构
            nodes = zTreeObj.transformToArray(nodes);

            //还原所有高亮属性为false
            for (let i in nodes) {
                nodes[i].highLight = false;
                zTreeObj.updateNode(nodes[i]);
            }
            treeNode.highLight = true;//设置高亮
            zTreeObj.updateNode(treeNode);//更新节点
            this.oid = treeNode.id;
            this.selectNoRole();//查询公司待选人员

        },
        fontCss: function (treeId, treeNode) {
            // 根据高亮标记设置高亮红色
            return treeNode.highLight ? {"color": "red"} : {"color": "black"}

        },
        changeDataScope: function () {
            if (this.officeTreeObj != '' && this.role.dataScope != 9) {
                this.officeTreeObj = '';
                $("#treeSelectOfficeEdit").css("display", "none");
            } else if (this.role.dataScope == 9) {
                this.initOfficeTree();
            }
        }

    },
    created: function () {
        this.role = parent.layer.obj;
        this.initTree();
        if (this.role.dataScope == 9) {
            this.initOfficeTree();//初始化公司树   用于跨机构授权处理
        }
    }
});